import Vue from 'vue'
import VueRouter from 'vue-router'
import {setWechatTitle, setLocalstorage, getLocalstorage} from './common/js/common'
/* import store from './store' */
Vue.use(VueRouter)

function load(component, componentDir = '') {
  return resolve => componentDir ? System.import(`components/${componentDir}/${component}.vue`) : System.import(`components/${component}.vue`)
}
const Newrouter = new VueRouter({
  // mode: 'history',
  /*
   * NOTE! VueRouter "history" mode DOESN'T works for Cordova builds,
   * it is only to be used only for websites.
   *
   * If you decide to go with "history" mode, please also open /config/index.js
   * and set "build.publicPath" to something other than an empty string.
   * Example: '/' instead of current ''
   *
   * If switching back to default "hash" mode, don't forget to set the
   * build publicPath back to '' so Cordova builds work again.
   */
  routes: [
    {path: '/', component: load('Index'), meta: {title: '悦龄旅游'}}, // Default
    {name: 'jounery', path: '/jounery', component: load('index', 'jounery'), meta: {title: '悦龄旅游'}},
    {name: 'jouneryDetail', path: '/jounery/detail/:id', component: load('detail', 'jounery')},
    {
      name: 'userCenter',
      path: '/userCenter/',
      component: load('index', 'userCenter'),
      meta: {title: '悦龄会员'},
      children: [
        {
          name: 'history',
          path: 'history',
          component: load('history', 'userCenter/history'),
          meta: {title: '悦龄足迹'}
        }
      ]
    },
    {name: 'address', path: '/address', component: load('address', 'userCenter')},
    {
      name: 'returnMoney', path: '/returnMoney/', component: load('returnReason', 'myOrder'), meta: {title: '退款原因'}
    },
    {name: 'login', path: '/login', component: load('index', 'logIn'), meta: {title: '悦龄会会员'}},
    {
      name: 'myOrder',
      path: '/myOrder/',
      component: load('index', 'myOrder'),
      meta: {title: '悦龄订单'},
      children: [
        {
          // 当 /userCenter/:id/baseInfo 匹配成功，
          name: 'activity_order',
          path: 'activity',
          component: load('activity', 'myOrder'),
          meta: {title: '悦龄订单'}
        },
        {
          // 当 /userCenter/:id/baseInfo 匹配成功，
          name: 'goods_order',
          path: 'goods',
          component: load('goods', 'myOrder'),
          meta: {title: '悦龄订单'}
        }
      ]
    },
    {name: 'payment', path: '/payment/:id', component: load('index', 'payment'), meta: {title: '悦龄订单'}}, // 支付
    {
      name: 'goods',
      path: '/goods/',
      component: load('index', 'goods'),
      meta: {title: '悦龄好物'}
    },
    {
      // 当 /userCenter/:id/baseInfo 匹配成功，
      name: 'goodsDetail',
      path: '/goods/detail/:id',
      component: load('detail', 'goods')
    }, // 好物
    {
      name: 'fun',
      path: '/fun/',
      component: load('index', 'activity'),
      meta: {title: '悦龄活动'}
    }, // 好玩
    {
      name: 'funDetail',
      path: '/fun/detail/:id',
      component: load('detailOne', 'activity'),
      children: [
        {
          name: 'funSingup22',
          path: '/singup/:id',
          component: load('singup', 'activity')
        }
      ]
    }, // 好玩详情
    {
      name: 'funSingup',
      path: '/fun/singup/:id',
      component: load('singup', 'activity')
    }, // 好玩报名
    {
      name: 'placeOrder',
      path: '/placeOrder/',
      component: load('index', 'placeOrder'),
      meta: {}
    }, // 下订单
    {path: '*', component: load('Error404')} // Not found
  ]
})
Newrouter.beforeEach((to, from, next) => {
  if (to.meta.title) {
    setWechatTitle(to.meta.title)
  }
  let fr = to.query.fr
  if (fr) {
    setLocalstorage('fr', fr)
  }
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    let islogin = getLocalstorage('islogin')
    if (!islogin) {
      next({
        path: '/login/',
        query: {redirect: to.fullPath}
      })
    }
    else {
      next()
    }
  }
  else {
    next() // 确保一定要调用 next()
  }
})
export default Newrouter
